package com.ygp.lock.rlock;

import com.ygp.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2021/11/15 <br>
 */
@Slf4j(topic = "c.bigRoom")
public class DeadLock {
    Object lockA = new Object();
    Object lockB = new Object();

    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
        deadLock.method1();
        deadLock.method2();
    }

    public void method1() {
        Thread t1 = new Thread(() -> {
            synchronized (lockA) {
                log.debug("lock 1");
                Sleeper.sleep(1);
                synchronized (lockB) {
                    log.debug("lock 2");
                    log.debug("操作...");
                }
            }
        }, "t1");
        t1.start();
    }

    public void method2() {
        Thread t2 = new Thread(() -> {
            synchronized (lockB) {
                log.debug("lock 2");
                Sleeper.sleep(1);
                synchronized (lockA) {
                    log.debug("lock 1");
                    log.debug("操作...");
                }
            }
        }, "t2");
        t2.start();
    }
}
